<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *
 * Created on	:  Jul 14, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RCoreImporter::cls( "mvc.AbstractController" );
RClassesImporter::cls('dbo.dtos.AuctionDTO');
RClassesImporter::cls('dbo.dtos.VehicleDTO');
RClassesImporter::cls('dbo.dtos.PhotoDTO');
RClassesImporter::cls('dbo.dtos.CategoryDTO');
RClassesImporter::cls('dbo.dtos.UserDTO');
RClassesImporter::cls('dbo.dtos.BidDTO');

class AuctionFrontController extends AbstractController {

	var $_model;
	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('AuctionModel');
		$this->_model = new AuctionModel();
	}

	/**
	 * Act hien thi cac Auction Newest tren Homepage
	 */
	function display(){
		$myModel = $this->_model;
		$leftMenu = $this->_getLeftMenu(0);
		$rs = $myModel->getNewestAuctions(12, 1);

		$newestHtml = '';
		foreach ($rs->getRecordSet() as $auction){
			$photoDto = $myModel->getPhotoFromGallery($auction->thumbnail);
			$thumb = RubikHelper::checkAndAddImageURL($photoDto->photo_url,145,90);
			$height = 90;
			$width = 145;	
			
			$link_detail = SystemSite::urlRoute('?app=auction&act=detailAuction&auctionID='.$auction->auction_id);
			//			$city = $myModel->getCity($auction->city_id);
			$state = $myModel->getState($auction->state_id);
			$type = $myModel->getCateName($auction->cate_id);
			$damage = $myModel->getDamage($auction->damage_id);

			eval('$oneAuction = "'.SysApplication::loadTemplate('front/auctionItem').'";');
				
			$newestHtml .= $oneAuction;
		}
		eval ( 'echo "' . SysApplication::loadTemplate ( "front/newest" ) . '";' );
	}


	/**
	 * Act hien thi thong tin chi tiet cua 1 Auction va thuc hien Bid
	 */
	function detailAuction() {
		$myModel = $this->_model;

		$auction_id = SysRequest::getString("auctionID");
		$auctionDto = $this->_getAuctionDto($auction_id);
		$vehicleDto = $myModel->getVehicleDto($auctionDto->vehicle_id);
		$ext_folder = SYS_ROOT_URL."ext";
		//		RubikHelper::debugPrePrint($auctionDto);
		//		RubikHelper::debugPrePrint($vehicleDto);

		//-------------------company tab---
		$companyTab = $this->_getCompanyTab($auctionDto->owner_id);

		//-------------------video tab---
		$videoTab = $this->_getVideoTab(RubikHelper::checkAndAddURL($vehicleDto->video));
		
		//-------------------spec tab---
//		$photoDto = $myModel->getPhotoFromGallery($vehicleDto->thumbnail);
//		$thumbnail = RubikHelper::checkAndAddImageURL($photoDto->photo_url);

		//----gallery----
		$arrowPre = RubikHelper::checkAndAddImageURL(SYS_CUR_TMPL_URL.'images/arrow-prev.png');
		$arrowNex = RubikHelper::checkAndAddImageURL(SYS_CUR_TMPL_URL.'images/arrow-next.png');
		$arrowPreLink = '#';
		$arrowNexLink = '#';
		$RECORD_PER_PAGE = 12;
		$page = SysRequest::getInt('page',1);
		$totalPhoto = 0;
		$oneImageUrl = '';
		$listPhotos = $this->_getPhotosVehicle($vehicleDto->vehicle_id, $vehicleDto->vehicle_name, $RECORD_PER_PAGE, $page, $totalPhoto, $oneImageUrl);
		if ($totalPhoto > $RECORD_PER_PAGE){
			if ($page > 1){
				$arrowPreLink = SystemSite::urlRoute('?app=auction&act=detailAuction&auctionID='.$auction_id.'&page='.($page-1));
			}
			if ($page < $totalPhoto/12 ){
				$arrowNexLink = SystemSite::urlRoute('?app=auction&act=detailAuction&auctionID='.$auction_id.'&page='.($page+1));
			}
		}

		$leftMenu = $this->_getLeftMenu($vehicleDto->cate_id);
		//		RubikHelper::debugPrePrint($vehicleDto->cate_id);

		$cate = $myModel->getVehicleType($vehicleDto->cate_id);
		$make = $myModel->getMake($vehicleDto->maker_id);
		$rs_model = $myModel->getModelGroupDetail($vehicleDto->model_id);
		$model_group = $rs_model->model_group;
		$model_detail = $rs_model->model_detail;

		$damage_id = $vehicleDto->damage_id;
		if ($damage_id != 0 && $damage_id!= ''){
			$damage = $myModel->getDamage($damage_id);
		}
		$damage_id2 = $vehicleDto->secondary_damage_id;

		if ($damage_id2 != 0 && $damage_id2 != ''){
			$damage2 = $myModel->getDamage($damage_id2);
		}

		if ($vehicleDto->haskey == 1){
			$hasKey = 'Yes';
		} else {
			$hasKey = 'No';
		}

		$saletitle_id = $vehicleDto->sale_title_type_id;
		if ($saletitle_id != 0 && $saletitle_id!= ''){
			$saletitle = $myModel->getSaleTitleType($saletitle_id);
		}

		$drive_id = $vehicleDto->drive_id;
		if ($drive_id != 0 && $drive_id!= ''){
			$drive = $myModel->getDriveType($drive_id);
		}

		$fuel_id = $vehicleDto->fuel_id;
		if ($fuel_id != 0 && $fuel_id!= ''){
			$fuel = $myModel->getFuelType($fuel_id);
		}

		$state_id = $auctionDto->state_id;
		if ($state_id != 0 && $state_id!= ''){
			$state = $myModel->getState($state_id);
		}

		if ($vehicleDto->runs_drives == 1){
			$runs_drives = 'Yes';
		} else {$runs_drives = 'No';}

		//get Other Auctions from this Company
		$otherAuctions = $this->_getOtherCurrentAuctions($auctionDto->owner_id, 5, 1);


		//----bid area-----
		
		//xu ly voi client da dang nhap
		if (SysAuthenticate::isLogged()){
			$bider = SysAuthenticate::getUserId();
			$firstBid = $this->_isFirstBid( $bider, $auction_id);
			$bidFee = $auctionDto->fee_bid;
			$isWaitDeposit = ($bidFee > 0);
	
			$guideline = $this->_getGuideline($bider, $auction_id, $isWaitDeposit);
			$confirmDeposit = $this->_getConfirmDeposit($bider, $auction_id, $auctionDto->fee_bid);
			$infoSuccess = $this->_getInfoSuccess();
			$bidProcessLink = SystemSite::urlRoute('?app=auction&act=bid');
	
	
			//kiem tra khong cho phep member tu BID auction cua minh.
			if ($bider == $auctionDto->owner_id){
				$buttonBid = '<a href="#" onclick="alert(\''.SysDBLanguage::_('You can not bid your auction.').'\');"><input type="button" class="bt" value="Bid now" /></a>';
			}
			else if (!$this->_isStart($auctionDto->start_date)){	//kiem tra bidder co bi block boi owner khong
				$buttonBid = '<a href="#" onclick="alert(\''.SysDBLanguage::_('This auction has not started yet.').'\');"><input type="button" class="bt" value="Bid now" /></a>';
			} 
			else if ($this->_isBlock($bider, $auctionDto->owner_id)){	//kiem tra auction da de gio` duoc phep BID chua (start_date)
				$buttonBid = '<a href="#" onclick="alert(\''.SysDBLanguage::_('You has blocked by this owner.').'\');"><input type="button" class="bt" value="Bid now" /></a>';
			} 
			else{	
				if ($firstBid){		//truong hop member tham gia BID auction nay lan dau tien
					$buttonBid = '<a href="#" onclick="activePanel1();"><input type="button" class="bt" value="Bid now" /></a>';
				}
				else {	//truong hop FEE BID cua Auctuon > 0  => hien thong bao DEPOSIT
					if ($bidFee > 0){
						$buttonBid = '<a href="#" onclick="activePanel2();"><input type="button" class="bt" value="Bid now" /></a>';
					}
					else {	//cac truong hop con lai thi xu ly va hien thong bao BID thanh cong
						$buttonBid = $this->_getButtonProcess($bider, $auction_id, 'false');
					}
				}
			}
		} else {
			$buttonBid = '<a href="#" onclick="alert(\'You must sign in to bid.\');"><input type="button" class="bt" value="Bid now" /></a>';
		}
		
		eval ( 'echo "' . SysApplication::loadTemplate ( "front/detailAuction" ) . '";' );
	}


	/**
	 * Act xu ly cho LEFT-MENU
	 */
	function cate(){
		$cate_id = SysRequest::getString('cate');
		$page = SysRequest::getInt('page',1);
		$RECORD_PER_PAGE = 10;	//5 test
		$myModel = $this->_model;
		$cateDto = $myModel->getCategoryDetails($cate_id);
		$icon = RubikHelper::checkAndAddImageURL($cateDto->icon);
		$totalAuction = 0;
		$from = ''; $to = '';
		$tableAuction = $this->_getAuctionCate($cate_id, $RECORD_PER_PAGE, $page, $totalAuction);
		$this->_getFromToLink($from, $to, $totalAuction, $page, $RECORD_PER_PAGE);
		$leftMenu = $this->_getLeftMenu($cate_id);

		//pagination
		if( $totalAuction > $RECORD_PER_PAGE) {
			$url = SystemSite::urlRoute( '?app=auction&act=cate&cate='.$cate_id.'&page');
			$pagination = $this->_getPagination($totalAuction, $RECORD_PER_PAGE, $url, $page);
		}
		eval ( 'echo "' . SysApplication::loadTemplate ( "front/cate" ) . '";' );
	}

	/**
	 * Act hien thi thong tin Owner (company) va danh sach cac Auction cua owner do
	 */
	function owner(){
		$myModel = $this->_model;
		$owner_id = SysRequest::getString('ownerID');
		$page = SysRequest::getInt('page',1);
		$RECORD_PER_PAGE = 10;
		$totalAuction = 0;
		$from = '';
		$to = '';

		$leftMenu = $this->_getLeftMenu();
		$userDto = $myModel->getUserDto($owner_id);
		$photo = RubikHelper::checkAndAddImageURL($userDto->photo);
		$city = $myModel->getCity($userDto->city_id);
		$state = $myModel->getState($userDto->state_id);
		$howtobuyLink = '#';
		$termsconditionsLink = '#';

		$tableAuction = $this->_getAuctionOwner($owner_id, $RECORD_PER_PAGE, $page, $totalAuction);
		$this->_getFromToLink($from, $to, $totalAuction, $page, $RECORD_PER_PAGE);

		//pagination
		if( $totalAuction > $RECORD_PER_PAGE) {
			$url = SystemSite::urlRoute( '?app=auction&act=owner&ownerID='.$owner_id.'&page');
			$pagination = $this->_getPagination($totalAuction, $RECORD_PER_PAGE, $url, $page);
		}
		eval ( 'echo "' . SysApplication::loadTemplate ( "front/owner" ) . '";' );
	}


	/**
	 * Ham xu ly cho ajax - thuc hien BID, gui mail cho owner
	 */
	function bid(){
		$bid_amount = SysRequest::getString('bid_amount');
		$bidder_id = SysRequest::getString('bidder');
		$auction_id = SysRequest::getString('auction_id');
		$deposit = SysRequest::getString('deposit');	// 'true' &  'false'

		$myModel = $this->_model;
		$auctionDto = $myModel->getAuctionDetails($auction_id);
		$vehicleDto = $myModel->getVehicleDto($auctionDto->vehicle_id);
		$ownerDto = $myModel->getUserDto($auctionDto->owner_id);
		$bidderDto = $myModel->getUserDto($bidder_id);
			
		//luu BID vao table "bids"
		$bidDto = new BidDTO();
//		$bidDto->setAllNullValues();
		$bidDto->user_id = $bidder_id;
		$bidDto->auction_id = $auction_id;
		$bidDto->bid_amount = $bid_amount;
		if ($deposit == 'true'){
			$bidDto->bid_status = 'waiting';
			//truong hop nay phai cho owner approve thi luc do moi update table "Auctions"
		} else {
			$bidDto->bid_status = 'approved';
				
			//cap nhat table "auctions"
			$myModel = $this->_model;
			$auctionDto = $myModel->getAuctionDetails($auction_id);
			if ($auctionDto->current_bid < $bid_amount){
				$auctionDto->current_bid = $bid_amount;
				SysDatabase::saveDTO($auctionDto);
			}
		}
		SysDatabase::saveDTO($bidDto);

		//send email cho owner
    	eval('$mailBody ="'.SysApplication::loadTemplate('front/mail').'";');
	    RCoreImporter::cls('helper.RMailHelper');
	    RMailHelper::sentMail($ownerDto->email, "[Auction.com] Has new bid for ".$vehicleDto->vehicle_name, $mailBody);

		echo $bid_amount."----".$bidder_id."-----".$auction_id."-----".$deposit;
	}


	function edit(){
		eval ( 'echo "' . SysApplication::loadTemplate ( "front/edit" ) . '";' );
	}

	function upload(){
		eval ( 'echo "' . SysApplication::loadTemplate ( "front/upload" ) . '";' );
	}
	
	/**
	 * 
	 * Make alias for loadNextMonthAuctions()
	 */
	function monthly_auctions() {
		return $this->loadNextMonthAuctions();
	}

	/**
	 *
	 * Monthly auctions
	 */
	function loadNextMonthAuctions() {
		$RECORD_PER_PAGE = 6;
		$myModel = $this->_model;
		$leftMenu = $this->_getLeftMenu(0);
		$page = SysRequest::getVar('page', 1);
		$rs = $myModel->getNextMonthAuctions($RECORD_PER_PAGE, $page);

		$height = 90;
		$width = 145;
		$title = SysDBLanguage::_($myModel->getNextMonth().' auctions');
		$contentHtml = '';

		foreach ($rs->getRecordSet() as $auction){
			$photoDto = $myModel->getPhotoFromGallery($auction->thumbnail);
			$thumb = RubikHelper::checkAndAddImageURL($photoDto->photo_url, $width, $height);
				
			$link_detail = SystemSite::urlRoute('?app=auction&act=detailAuction&auctionID='.$auction->auction_id);
			$state = $myModel->getState($auction->state_id);
			$type = $myModel->getCateName($auction->cate_id);
			$damage = $myModel->getDamage($auction->damage_id);
				
			eval('$oneAuction = "'.SysApplication::loadTemplate('front/auctionItem').'";');
				
			$contentHtml .= $oneAuction;
		}
		$linker = new SiteResource();
		$linker->application = 'auction';
		$linker->action = 'monthly_auctions';
		$linker->task = 'page';
		$url = $linker->getLink();
		$pager = SysTemplate::getPaginationInstance($rs->getTotalRecords(), $RECORD_PER_PAGE, $url);
		$pagingHtml = $pager->renderPager($page);
		require SysApplication::getTemplatePath('front/monthly');
	}

	/**
	 * 
	 * Load auctions by state and city
	 */
	function loadAuctionsByStateCity() {
		$stateId = $_POST['state_id'];
		$cityId = $_POST['city_id'];
		$RECORD_PER_PAGE = 12;
		$myModel = $this->_model;
		$leftMenu = $this->_getLeftMenu(0);
		$page = SysRequest::getVar('page', 1);
		$rs = $myModel->getAuctionsByStateCity($stateId, $cityId, $RECORD_PER_PAGE, $page);

		$height = 90;
		$width = 145;
		$stateName = $myModel->getState($stateId);
		$cityName = $myModel->getCity($cityId);
		$title = 'AUCTIONS';
		if(!$stateId) {
			$msg = SysDBLanguage::_("Please select state and city");
		}
		if(!$msg) {
			if(!$rs->getTotalRecords()) {
				$msg = SysDBLanguage::_("No auction found in ").$stateName;
			}
		}
		$contentHtml = '';

		foreach ($rs->getRecordSet() as $auction){
			$photoDto = $myModel->getPhotoFromGallery($auction->thumbnail);
			$thumb = RubikHelper::checkAndAddImageURL($photoDto->photo_url, $width, $height);
				
			$link_detail = SystemSite::urlRoute('?app=auction&act=detailAuction&auctionID='.$auction->auction_id);
			$state = $myModel->getState($auction->state_id);
			$type = $myModel->getCateName($auction->cate_id);
			$damage = $myModel->getDamage($auction->damage_id);
				
			eval('$oneAuction = "'.SysApplication::loadTemplate('front/auctionItem').'";');
				
			$contentHtml .= $oneAuction;
		}
		$linker = new SiteResource();
		$linker->application = 'auction';
		$linker->action = 'loadAuctionsByStateCity';
		$linker->task = 'page';
		$url = $linker->getLink();
		$pager = SysTemplate::getPaginationInstance($rs->getTotalRecords(), $RECORD_PER_PAGE, $url);
		$pagingHtml = $pager->renderPager($page);
		require SysApplication::getTemplatePath('front/monthly');
	}

	//----------------------- private functions ---------------------------
	function _getAuctionDto($id){
		$myModel = $this->_model;
		$myDto = $myModel->getAuctionDetails($id);
		return $myDto;
	}

	//lay cac auction cua 1 cate
	function _getAuctionCate($cate_id, $RECORD_PER_PAGE = 10, $page = 1, &$totalAuction){
		$myModel = $this->_model;
		$rs = $myModel->getAuctionsCate($cate_id, $RECORD_PER_PAGE, $page);
		$totalAuction = $rs->getTotalRecords();
		$tableAuction = $this->_getListAuction($rs);
		return $tableAuction;
	}

	//lay cac auction cua 1 owner
	function _getAuctionOwner($owner_id, $RECORD_PER_PAGE = 10, $page = 1, &$totalAuction){
		$myModel = $this->_model;
		$rs = $myModel->getOtherCurrentAuctions($owner_id, $RECORD_PER_PAGE, $page);
		$totalAuction = $rs->getTotalRecords();
		$tableAuction = $this->_getListAuction($rs);
		return $tableAuction;
	}

	//lay cac 5 auction khac cua 1 owner
	function _getOtherCurrentAuctions($owner_id, $RECORD_PER_PAGE = 5, $page = 1){
		$myModel = $this->_model;
		$rs = $myModel->getOtherCurrentAuctions($owner_id, $RECORD_PER_PAGE, $page);
		$tableAuction = $this->_getListAuction($rs, false);
		$viewMoreLink = SystemSite::urlRoute('?app=auction&act=owner&ownerID='.$owner_id);
		eval('$html="'.SysApplication::loadTemplate('front/otherAuction').'";');
		return $html;
	}

	//hien thi danh sach cac auction theo dang Table
	function _getListAuction($rs, $sortHeader = true){
		$myModel = $this->_model;
		if( $rs->getTotalRecords() > 0){
			$count = 0;
			$headerTable = $this->_getHeaderTable($sortHeader);
			foreach ($rs->getRecordSet() as $auction){
				$class = '';
				if ($count % 2 ==0){
					$class = 'grey';
				}
				$count++;
				$thumb_dto = $myModel->getPhotoFromGallery($auction->thumbnail);
				$thumb = RubikHelper::checkAndAddImageURL($thumb_dto->photo_url);
				$link_detail = SystemSite::urlRoute('?app=auction&act=detailAuction&auctionID='.$auction->auction_id);
				$link_owner_page = SystemSite::urlRoute('?app=auction&act=owner&ownerID='.$auction->owner_id);

				$content_other_auction .= '<tr class="'.$class.'">';
				$content_other_auction .= '<td><a href="'.$link_detail.'"><img src="'.$thumb.'" width="86" height="49" alt="'.$auction->vehicle_name.'" /></a></td>';
				$content_other_auction .= '<td align="center">'.$auction->year.'</td>';
				$content_other_auction .= '<td align="center">'.$myModel->getMake($auction->maker_id).'</td>';
				$content_other_auction .= '<td align="center">'.$myModel->getModel($auction->model_id).'</td>';
				$content_other_auction .= '<td align="center">'.$auction->start_date.'</td>';
				$content_other_auction .= '<td align="center">'.$auction->odometer.'&nbsp;</td>';
				$content_other_auction .= '<td align="center"><a href="'.$link_owner_page.'">'.$myModel->getNameOwner($auction->owner_id).'</a></td>';
				$content_other_auction .= '<td align="center" class="current-bid">$'.$auction->current_bid.'</td>';
				$content_other_auction .= '<td align="center"><a href="'.$link_detail.'" class="a-bt">Bid</a>';
				$content_other_auction .= '</tr>';
			}
		}
		eval('$html="'.SysApplication::loadTemplate('front/tableAuction').'";');
		return $html;
	}

	//Header cua table hien thi cac auction
	function _getHeaderTable($sort = true){
		if ($sort){
			$image_url = RubikHelper::checkAndAddImageURL(SYS_CUR_TMPL_URL.'images/arrow-down-blue.png');
			$image_sort = '<img src="'.$image_url.'" alt="sort" />';
			$link_sort_year = '#';
			$link_sort_make = '#';
			$link_sort_model = '#';
			$link_sort_saleDate = '#';
			$link_sort_odometer = '#';
			$link_sort_currentBid = '#';
			$link_sort_owner = '#';
		}
		$html .= '<tr>';
		$html .= '<th width="13%">Images</th>';
		$html .= '<th width="7%">Year&nbsp;<a href="'.$link_sort_year.'">'.$image_sort.'</a></th>';
		$html .= '<th width="12%">Make&nbsp;<a href="'.$link_sort_make.'">'.$image_sort.'</a></th>';
		$html .= '<th width="12%">Model&nbsp;<a href="'.$link_sort_model.'">'.$image_sort.'</a></th>';
		$html .= '<th width="13%">Sale Date&nbsp;<a href="'.$link_sort_saleDate.'">'.$image_sort.'</a></th>';
		$html .= '<th width="13%">Odometer&nbsp;<a href="'.$link_sort_odometer.'">'.$image_sort.'</a></th>';
		$html .= '<th width="13%">Owner&nbsp;<a href="'.$link_sort_owner.'">'.$image_sort.'</a></th>';
		$html .= '<th width="11%">Current Bid&nbsp;<a href="'.$link_sort_currentBid.'">'.$image_sort.'</a></th>';
		$html .= '<th width="6%">&nbsp;</th>';
		$html .= '</tr>';
		return $html;
	}

	//lay cac photo cua 1 auction de dua vao gallery (dung LightBox de hien thi hinh to)
	function _getPhotosVehicle($vehicle_id, $vehicle_name, $RECORD_PER_PAGE = 12, $page = 1, &$totalImage, &$oneImageUrl){
		$model = $this->_model;
		$rs = $model->getPhotosVehicle($vehicle_id, $RECORD_PER_PAGE, $page);

		$totalImage = $rs->getTotalRecords();
		if( $totalImage > 0){
			$html = '<table cellpadding="3"><tr>';
			$count = 0;
			foreach ($rs->getRecordSet() as $photo){
				$count++;
				$image_url_thumb = RubikHelper::checkAndAddImageURL($photo->url, 88, 65);
				$image_url = RubikHelper::checkAndAddImageURL($photo->url);
				$oneImageUrl = $image_url; 
				
				//doan code dung JS binh thuong 				
				$images = '<a href="javascript:void(0)" onclick="changeImg(\''.$image_url.'\');"><img id="small_picture'.$count.'" src="'.$image_url_thumb.'" class="border-img" height="65" width="88" /></a>';

				//doan code dung LightBox de hien thi hinh to  (nguoc yeu cau - tam thoi bo)
//				$images = '<a href="'.$image_url.'" title="'.$vehicle_name.'"><img alt="'.$vehicle_name.'" title="'.$vehicle_name.'" src="'.$image_url_thumb.'" border="0" class="border-img" height="65" width="88"/></a>';

				$html .= '<td width="33%">'.$images.'</td>';
				
				if ($count % 3 == 0){
					$html .= '</tr><tr>';
				}
			}
			$html .= '</tr></table>';
		}
		return $html;
	}


	//hien thi LEFT-MENU
	function _getLeftMenu($cateSelected = 0){
		$myModel = $this->_model;
		$rs = $myModel->getCategory();
		$content_left_menu = '';
		foreach ($rs as $cate){
			$icon = RubikHelper::checkAndAddImageURL($cate->icon);
			$url = SystemSite::urlRoute('?app=auction&act=cate&cate='.$cate->category_id.'&page=1');
			if ($cateSelected != $cate->category_id){
				$content_left_menu .= '<li><img src="'.$icon.'" alt="'.$cate->name.'" /><a href="'.$url.'">'.$cate->name.'</a></li>';
			} else {
				$content_left_menu .= '<li class="selected"><img src="'.$icon.'" alt="'.$cate->name.'" /><a href="'.$url.'">'.$cate->name.'</a></li>';
			}
		}
		eval('$html="'.SysApplication::loadTemplate('front/leftMenu').'";');
		return $html;
	}

	//hien thi phan trang
	function _getPagination($totalRecord, $record_per_page, $url, $page){
		$url = str_replace(":age","page",$url);
		$pager 	= 	SysTemplate::getPaginationInstance( $totalRecord, $record_per_page, $url);
		return '<center>'.$pager->renderPager($page).'</center>';
	}


	//thiet lap gia tri cho gia tri dieu huong (from-to OF total) cua table hien thi cac auction
	function _getFromToLink(&$from, &$to, $totalAuction, $page, $RECORD_PER_PAGE){
		if ($totalAuction == 0){
			$from = 0;
		} else {
			$from = ($page-1)*$RECORD_PER_PAGE + 1;
		}
		if ($totalAuction <= $RECORD_PER_PAGE){
			$to = $totalAuction;
		} else {
			$to = $page*$RECORD_PER_PAGE;
			if ($to > $totalAuction){
				$to = $totalAuction;
			}
		}
	}


	//tao "popup" Guideline cua qua trinh BID
	function _getGuideline($user_id, $auction_id, $isWaitDeposit){
		$myModel = $this->_model;
		$content = $myModel->getStaticContent('guideline');
//		$content = 'Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline
//		Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline Guideline ';
		SysDBLanguage::_($content);
		
		$button_cancel = '<a onclick="deactivePanel1()"><input id="btCancel1" class="bt" type="button" value="Cancel" /></a>';

		if ($isWaitDeposit){	//truong hop FEE BID > 0 thi hien POPUP2
			$button_ok = '<a onclick="deactivePanel1(); activePanel2();"><input id="btOk1" class="bt" type="button" value="OK" />';
		}
		else {	//nguoc lai thi thuc hien BID va thong bao thanh cong
			$button_ok = $this->_getButtonProcess($user_id, $auction_id, 'false');
		}

		$html = '<br/><br/><br/><br/>
				 <div align="center">
					<table border="1" width="50%">
						<tr>
							<td><h2>'.$content.'</h2></td>
						</tr>
						<tr>
							<td><div align="center">'.$button_cancel.'&nbsp;&nbsp;'.$button_ok.'</div></td>
						</tr>
					</table>
				</div>';
		return $html;
	}

	//tao "popup" Deposit cua qua trinh BID
	function _getConfirmDeposit($user_id, $auction_id, $fee){
		$content = 'ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit
		ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit 
		ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ConfirmDeposit ';

		$content = '<h2>This auction has a fee bid: <font color="red">$'.$fee.'.</font><br/><br/> Are you sure to bid this auction?</h2>';
		
		$button_cancel = '<a onclick="deactivePanel2()"><input id="btCancel1" class="bt" type="button" value="Cancel" /></a>';
		$button_ok = $this->_getButtonProcess($user_id, $auction_id, 'true');

		$html = '<br/><br/><br/><br/>
				 <div align="center">
					<table border="1" width="50%">
						<tr>
							<td>'.$content.'</td>
						</tr>
						<tr>
							<td><div align="center">'.$button_cancel.'&nbsp;&nbsp;'.$button_ok.'</div></td>
						</tr>
					</table>
				</div>';
		return $html;
	}

	//tao "popup" Success cua qua trinh BID
	function _getInfoSuccess(){
		$content = '<h2>You are bidded successfully.</h2> ';
		$button_ok = '<a href="#" onclick="deactivePanel3();"><input id="btOk1" class="bt" type="button" value="OK" />';
		$html = '<br/><br/><br/><br/>
				 <div align="center">
					<table border="1" width="50%">
						<tr>
							<td><div align="center">'.$content.'</div></td>
						</tr>
						<tr>
							<td><div align="center">'.$button_ok.'</div></td>
						</tr>
					</table>
				</div>';
		return $html;
	}

	//tao button OK -> xu ly thao tac BID
	function _getButtonProcess($user_id, $auction_id, $deposit){
		//truong hop co Deposit ('true') thi xu ly BID cua member o status WAITING
		//nguoc lai thi thiet lap BID o status OK luon
		$button = '<a href="#" onclick="processBid('.$user_id.', '.$auction_id.', '.$deposit.');"><input id="btOk1" class="bt" type="button" value="OK" />';
		return $button;
	}

	//kiem tra 1 user co phai thuc hien BID dau tien cho 1 auction khong
	function _isFirstBid($user_id, $auction_id){
		$myModel = $this->_model;
		return $myModel->checkFirstBid($user_id, $auction_id);
	}

	//kiem tra 1 auction da start chua
	function _isStart($start_date){
//		RubikHelper::debugPrePrint($start_date);
//		RubikHelper::debugPrePrint(date("Y-m-d H:i:s"));
//		RubikHelper::debugPrePrint($start_date <= date("Y-m-d H:i:s"));
		return $start_date <= date("Y-m-d H:i:s");
	}
	
	//kiem tra 1 bidder co bi block boi owner khong
	function _isBlock($bidder, $owner){
		$myModel = $this->_model;
		$result = $myModel->checkBlocked($bidder, $owner);
		return $result != 0;
	}
	
	//tao noi dung cho TAB company
	function _getCompanyTab($user_id){
		$myModel = $this->_model;
		$userDto = $myModel->getUserDto($user_id);
		$photoURL = $userDto->photo;
    	if ($photoURL != ""){
			$photoURL = RubikHelper::checkAndAddImageURL($photoURL);    		
    	} else {
			$photoURL = RubikHelper::checkAndAddImageURL(SYS_CUR_TMPL_URL."images/blank-avatar.png");
    	}
		$state = $this->_getStateName($userDto->state_id);
    	$city = $this->_getCityName($userDto->city_id);
    	
		ob_start();
		require SysApplication::getTemplatePath('front/companyview');
		$html = ob_get_contents();
		ob_end_clean();
		
		return $html;
	}

	//tao noi dung cho TAB video
	function _getVideoTab($flvFile){
		if ($flvFile != ''){
			$html = RHtmlHelper::createFlashVideoPlayer($flvFile,400,300);	
		} else {
			$html = '<h3><br/><br/>There isn\'t video for this vehicle.<br/><br/></h3>';
		}
		return $html;
	}
	
	function _getStateName($id){
		if ($id == '') return "";
		$myModel = $this->_model;
		return $myModel->getState($id);
	}
	
	function _getCityName($id){
		if ($id == '') return "";
		$myModel = $this->_model;
		return $myModel->getCity($id);
	}
}